import { Component, OnInit, ViewChild, ViewContainerRef, ComponentRef, ComponentFactoryResolver, EventEmitter } from '@angular/core';
import * as flatpickr from 'flatpickr';
import { zh } from "flatpickr/dist/l10n/zh.js";
import { AppPaginationComponent, PaginationModel } from "../../../../../widgets/pagination/pagination.component";
import { environment } from "../../../../../../environments/environment";
import * as _ from "lodash";
import { DataService } from '../../../../../services/data.service';
import { Util } from '../../../../../common/util';
import { SelectItem } from 'primeng/components/common/api';
import { RouteRevokeService } from './route-search.service';

@Component({
  selector: 'app-route-search',
  templateUrl: './route-search.component.html',
  styleUrls: ['./route-search.component.scss'],
  providers: [RouteRevokeService]
})
export class RouteSearchComponent implements OnInit {

  // 分页列表post数据
  public postBody = {
    pageIndex: 1,
    pageSize: 50,
    sOprDateStart: "",//发起日期
    sOprDateEnd: "",//结束日期
    nBatchNoStart: "",//开始批次
    nBatchNoEnd: "",//结束批次
    commitOprZh: "",//发起人
    sFileName: "",//文件名称
    sFundGroupds: "",//适用基金组
    sFundType: "",//适用基金类别
    f1: "",//内容
    sValid: "",//是否有效
    sAccountName: ""//账户名称
  }
  public sFundType: any = [];
  public sFundGroupds: any = [];
  //字典值列表
  public dictionaryList: any = {
    RISK_RECORD_TYPE: [],//属性
    NX_SOURCE: [],//条款出处
    RISK_ACCOUNT_TYPE: [],//适用基金组类别
    RISK_CTRL_METHOD: [],//事中控制方法
    RISK_CONTROL_AFTER: [],//事后监控方法
    MONITOR_REMARK: [],//风险等级
    RISK_CW_CTRL: [],//是否场外系统可控
    RISK_HS_CONTROL: []//是否事中系统可控
  }
  //适用基金组列表
  public groupList: any = [];
  public dictionaryInfo: SelectItem[];
  constructor(
    public dataService: DataService,
    public routeRevokeService: RouteRevokeService
  ) { }

  ngOnInit() {
    // 字典值
    this.dictionaryList = JSON.parse(localStorage.getItem("dictionaryList"));
    Util.loadSelect(this.dictionaryList.RISK_ACCOUNT_TYPE, 'NqdiiRouteFundType')
    // this.fundGroupList();
    //初始化适用基金组多选框,因为使用的不是字典值，所以通用方法无法使用
    this.fundGroupsSelect2();
  }
  //获取基金组信息列表
  fundGroupList() {
    let that = this;
    let getBody = {
      sType: 0
    }
    this.routeRevokeService.fundGroupList(getBody).subscribe(data => {
      if (data) {
        this.groupList = data;
        console.log(data);
      }
    })
  }
  //初始化适用基金组多选框
  fundGroupsSelect2() {
    let that = this;
    var html = '';
    // console.log(this.groupList)
    // setTimeout(() => {
    //   if (this.groupList.length == 0) {
    //     this.fundGroupsSelect2();
    //   } else {

        for (var i = 0; i < this.groupList.length; i++) {
          html += "<option value='" + this.groupList[i].nFgId + "'>" + this.groupList[i].sFgName + "</option>";
        }
        window['$']("#NqdiiRouteFundGroupds").html(html);

        window['$']('#NqdiiRouteFundGroupds').selectpicker({
          size: "8",
          actionsBox: "true",
          title: "请选择",
          selectAllText: "全选",
          deselectAllText: "取消全选",
          width: 182
        });
    //   }
    // });
  }
  //关闭modal并发射数据
  hideModal() {
    window['$']('#routeSearch').modal('hide');
    this.postBody.sFundType = this.sFundType.join(',');
    this.postBody.sFundGroupds = this.sFundGroupds.join(',');
    this.dataService.nQDIIRouteSearchInfo = this.postBody;
    this.dataService.nQDIIInputModelEmitter.emit('routeSearch');
  }
  //搜索查询
  searchQuery() {
    let that = this;
    if (Util.dateOrder(that.postBody.sOprDateStart, that.postBody.sOprDateEnd)) {
      if (that.postBody.nBatchNoEnd != '' && that.postBody.nBatchNoStart != '') {
        if (parseInt(that.postBody.nBatchNoEnd) > parseInt(that.postBody.nBatchNoStart)) {
          that.hideModal();
        } else {
          window['swal']("提示", "起始批次 须小于 结束批次", "warning");
        }
      } else {
        that.hideModal();
      }


    } else {
      window['swal']("提示", "发行日期 须早于 到期日期", "warning");
    }
  }
}
